/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.persistence.EntityManager;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import model.Contact;
import persistence.ContactDAO;
import persistence.PersistenceUtil;

/**
 *
 * @author hbriand
 * 
 * @class ContactForm
 * @desc The form to edit or create a new contact
 */
public class ContactForm implements WindowListener {
    private Contact currentContact;
    private JFrame frame;

    private JTextField nameTxt;
    private JTextField surnameTxt;
    private JTextField phoneTxt;
    private JTextField emailTxt;
    private JTextField imagePath;
    private ContactFormDelegate delegate;


    public ContactForm(Contact edit) {
        this.currentContact = edit;
        this.frame = new JFrame();
        this.draw();
    }

    public ContactForm() {
        this.currentContact = new Contact("", "", "", "", new ImageIcon());
        @SuppressWarnings("unused")
		EntityManager em = PersistenceUtil.getEM();
        (new ContactDAO()).persist(this.currentContact);
        PersistenceUtil.closeEM();
        this.frame = new JFrame();
        this.draw();
    }

    public void setContact(Contact c) {
        this.currentContact = c;
    }

    public void show() {
        this.frame.setVisible(true);
    }

    public void hide() {
        this.frame.setVisible(false);
    }

    private void draw() {
        this.frame.setSize(800, 600);
        this.frame.setLayout(new BorderLayout());
        this.frame.add(this.getLabelsInputsComponent(), BorderLayout.CENTER);
        this.frame.add(this.getActionButtons(), BorderLayout.PAGE_END);
    }

    public void setDelegate(ContactFormDelegate aThis) {
        this.delegate = aThis;
    }

    private Component getActionButtons() {
        JPanel actionButtons = new JPanel();
        actionButtons.setLayout(new FlowLayout());

        JButton add = new JButton("Save");
        add.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                currentContact.setEmail(emailTxt.getText());
                currentContact.setName(nameTxt.getText());
                currentContact.setSurname(surnameTxt.getText());
                currentContact.setPhone(phoneTxt.getText());
                currentContact.setPhoto(new ImageIcon(imagePath.getText()));
                hide();
                delegate.contactFormAdd(currentContact);
            }

        });
        actionButtons.add(add);

        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                hide();
                delegate.contactFormCancel();
            }
        });
        actionButtons.add(cancel);
        
        JButton addImage = new JButton("Add Picture");
        addImage.addActionListener(new ActionListener() {
        	
        	public void actionPerformed(ActionEvent e) {
        		JFileChooser chooser = new JFileChooser();
        	    // Note: source for ExampleFileFilter can be found in FileChooserDemo,
        	    // under the demo/jfc directory in the JDK.
        	    int returnVal = chooser.showOpenDialog(frame);
        	    if(returnVal == JFileChooser.APPROVE_OPTION) {
        	       System.out.println("You chose to open this file: " +
        	            chooser.getSelectedFile().getName());
        	       imagePath.setText(chooser.getSelectedFile().getAbsolutePath());
        	    }
        	}
        });
        actionButtons.add(addImage);

        return actionButtons;
    }

    private Component getLabelsInputsComponent() {
        JPanel liComp = new JPanel();
        liComp.setLayout(new BoxLayout(liComp, BoxLayout.PAGE_AXIS));

        JPanel namePnl = new JPanel(new FlowLayout());
        namePnl.add(new JLabel("Name :"));
        this.nameTxt = new JTextField(this.currentContact.getName(), 50);
        namePnl.add(nameTxt);
        liComp.add(namePnl);

        JPanel surnamePnl = new JPanel(new FlowLayout());
        surnamePnl.add(new JLabel("Surname :"));
        this.surnameTxt = new JTextField(this.currentContact.getSurname(), 50);
        surnamePnl.add(surnameTxt);
        liComp.add(surnamePnl);

        JPanel phonePnl = new JPanel(new FlowLayout());
        phonePnl.add(new JLabel("Phone number :"));
        this.phoneTxt = new JTextField(this.currentContact.getPhone(), 50);
        phonePnl.add(phoneTxt);
        liComp.add(phonePnl);

        JPanel emailPnl = new JPanel(new FlowLayout());
        emailPnl.add(new JLabel("Email :"));
        this.emailTxt = new JTextField(this.currentContact.getEmail(), 50);
        emailPnl.add(emailTxt);
        liComp.add(emailPnl);
        
        JPanel photoPnl = new JPanel(new FlowLayout());
        photoPnl.add(new JLabel("Picture :"));
        this.imagePath = new JTextField(50);
        this.imagePath.setEnabled(false);
        photoPnl.add(this.imagePath);
        liComp.add(photoPnl);

        return liComp;
    }

    public void windowOpened(WindowEvent e) {
    }

    public void windowClosing(WindowEvent e) {
        this.delegate.contactFormCancel();
    }

    public void windowClosed(WindowEvent e) {
    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowActivated(WindowEvent e) {
    }

    public void windowDeactivated(WindowEvent e) {
    }

}
